<?php
/**
 * Employee.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class Application_Model_Search_Index_Employee extends Application_Model_Search_Index_Abstract {
	
	/**
	 * Returns the directory where the index is stored.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @return string Directory where the index is stored.
	 */
	public static function getIndexDirectory(){
		
		return parent::getIndexDirectory() .'/employees';
	}
	
	/**
	 * updateDocument() - Update the index with the index data. Updating is done by removing any
	 * duplicates and then reinsert a new document.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @throws InvalidArgumentException
	 * @param Application_Model_Domain_Employee $employee Data to build the document to update from.
	 */
	public function updateDocument($employee){
		 
		if(! $employee instanceof Application_Model_Domain_Employee){
			throw new InvalidArgumentException(
				__METHOD__ .', this method only accepts an employee domain instance.'
			);
		}
		
		$document 			= $this->getNewDocumentInstance($employee);
		$index 				= $this->getIndex();
		
		$document->addField(Zend_Search_Lucene_Field::Text('firstname', $employee->getFirstName()));
		$document->addField(Zend_Search_Lucene_Field::Text('lastname', $employee->getLastName()));
		$document->addField(Zend_Search_Lucene_Field::Text('email', $employee->getEmail()));
		$document->addField(Zend_Search_Lucene_Field::Text('username', $employee->getUsername()));
		
		// Remove duplicates and reinsert updated document, this is how updating the index works.
		$this->_deleteDuplicates($employee);
		$index->addDocument($document);
	}
	
	/**
	 * Returns a value that is used to identify the document containing the
	 * indexdata.
	 *
	 * @author b.wenneker <b.wenneker@gmail.com>, 2010
	 * @param Application_Model_Domain_Employee $indexData
	 */
	public function getIdentifierFieldValue($indexData){
		
		return 'employee.'.$indexData->getId();
	}
}